//
//  CPAudioTools.m
//  CPAudioView
//
//  Created by collegepre on 16/12/12.
//  Copyright © 2016年 collegepre. All rights reserved.
//

#import "CPAudioTools.h"
#import <UIKit/UIKit.h>

@implementation CPAudioTools

+ (NSString *)audio_replaceHTMLJS:(NSString *)htmString{
    NSString *decodeJson = htmString;
    if (![decodeJson isEqualToString:@""]&& decodeJson != nil) {
        decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"//设置状态" withString:@" "];
        decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"//用户完成所有题目按钮可点击" withString:@" "];
        decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"//默认状态" withString:@" "];
        decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"//读取状态" withString:@" "];
        decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"//read" withString:@" "];
        decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"//set" withString:@" "];
        decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"//clear" withString:@" "];
        
        
        
        decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"//播放音频" withString:@" "];
        
        if ([decodeJson containsString:@"<div class=\"audio\""] ||[decodeJson containsString:@"<div class=\"audio"]||[decodeJson containsString:@"<div class = \"audio"]||[decodeJson containsString:@"<div class =\"audio"]||[decodeJson containsString:@"<div  class=\"audio"]||[decodeJson containsString:@"<div class= \"audio"])
        {
            decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"<script src=\"js/video.js\"></script>" withString:@""];
            
             decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"js/video.js" withString:@""];
            
            NSString *style = @"<style>.video-js{display:none;} </style></head>";
            
            decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"</head>" withString:style];
        }
        
        
        NSString *path = [[NSBundle bundleForClass:[CPAudioTools class]] pathForResource:@"audio_js.html" ofType:nil];
        NSData *JSdata = [NSData dataWithContentsOfFile:path];
        
        NSString *JS = [[NSString alloc] initWithData:JSdata encoding:NSUTF8StringEncoding];
        if (![JS isEqualToString:@""]&&JS != nil) {
            decodeJson = [decodeJson stringByReplacingOccurrencesOfString:@"</body>" withString:JS];
        }
    }
    return decodeJson;
}

+ (UIColor *)audio_colorWithHexString:(NSString *)hexString
{
    
    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#"withString: @""] uppercaseString];
    CGFloat alpha, red, blue, green;
    
    switch ([colorString length]) {
        case 3: // #RGB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 1];
            green = [self colorComponentFrom: colorString start: 1 length: 1];
            blue  = [self colorComponentFrom: colorString start: 2 length: 1];
            break;
        case 4: // #ARGB
            alpha = [self colorComponentFrom: colorString start: 0 length: 1];
            red   = [self colorComponentFrom: colorString start: 1 length: 1];
            green = [self colorComponentFrom: colorString start: 2 length: 1];
            blue  = [self colorComponentFrom: colorString start: 3 length: 1];
            break;
        case 6: // #RRGGBB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 2];
            green = [self colorComponentFrom: colorString start: 2 length: 2];
            blue  = [self colorComponentFrom: colorString start: 4 length: 2];
            break;
        case 8: // #AARRGGBB
            alpha = [self colorComponentFrom: colorString start: 0 length: 2];
            red   = [self colorComponentFrom: colorString start: 2 length: 2];
            green = [self colorComponentFrom: colorString start: 4 length: 2];
            blue  = [self colorComponentFrom: colorString start: 6 length: 2];
            break;
        default:
            
            @throw [[NSException alloc] initWithName:@"Invalid color value" reason:[NSString stringWithFormat:@"Color value \"%@\" is invalid.  It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB.", hexString] userInfo:nil];
            break;
    }
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    
}

+ (CGFloat)colorComponentFrom:(NSString *)string start:(NSUInteger)start length:(NSUInteger)length
{
    
    NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
    
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
    
    unsigned hexComponent;
    
    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
    
    return hexComponent/255.0;
    
}
@end
